<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="资产编号" prop="assetsNo">
        <el-input v-model="queryParams.assetsNo" placeholder="请输入资产编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="资产名称" prop="assetsName">
        <el-input v-model="queryParams.assetsName" placeholder="请输入资产名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="资产类型" prop="assetsType">
        <el-select v-model="queryParams.assetsType" placeholder="请选择" clearable @keyup.enter.native="handleQuery">
          <el-option v-for="dict in dict.type.cld_assets_type" :key="dict.value" :label="dict.label"
            :value="parseInt(dict.value)"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['cld:assets:add']">新增资产</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="assetsList" @selection-change="handleSelectionChange" border>
      <el-table-column label="资产编号" align="center" prop="assetsNo" />
      <el-table-column label="资产名称" align="center" prop="assetsName" />
      <el-table-column label="资产类型" align="center" prop="assetsType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_assets_type" :value="scope.row.assetsType" />
        </template>
      </el-table-column>
      <el-table-column label="资产图片" align="center" prop="assetsImg">
        <template slot-scope="scope">
          <image-preview :src="scope.row.assetsImg" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="资产型号" align="center" prop="assetsModel" />
      <el-table-column label="资产单位" align="center" prop="assetsUnit" />
      <el-table-column label="总数量" align="center" prop="assetsTotalNum" />
      <el-table-column label="购置单价(元)" align="center" prop="bulMoney" />
      <el-table-column label="购置总价(元)" align="center" prop="bulTotalMoney" />
      <el-table-column label="折旧期数" align="center" prop="assetsPeriodNumber" />
      <el-table-column label="创建人" align="center" prop="createName" />
      <el-table-column label="创建日期" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column> <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="220px;">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleAssetsRecord(scope.row)" v-hasPermi="['cld:CldAssetsRecord:list']">资产状况</el-button>
          <el-button size="mini" type="text" @click="handleDetailsAdd(scope.row)" v-hasPermi="['cld:CldAssetsRecord:add']">新增入库</el-button>
          <el-button size="mini" type="text" @click="handleAssetsDetails(scope.row)" v-hasPermi="['cld:assets:list']">资产明细</el-button>
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)" v-hasPermi="['cld:assets:edit']">编辑</el-button>
          <!--<el-button size="mini" type="text" @click="handleDelete(scope.row)" v-hasPermi="['cld:assets:remove']">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改资产信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" size="mini" label-position="top" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="资产名称" prop="assetsName">
              <el-input v-model="form.assetsName" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="资产型号" prop="assetsModel">
              <el-input v-model="form.assetsModel" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="品牌" prop="assetsBrand">
              <el-input v-model="form.assetsBrand" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">

          <el-col :span="8">
            <el-form-item label="资产类型" prop="assetsType">
              <el-select filterable v-model="form.assetsType" placeholder="请选择">
                <el-option v-for="dict in dict.type.cld_assets_type" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="折旧期数" prop="assetsPeriodNumber">
              <el-input v-model="form.assetsPeriodNumber" placeholder="请输入"
                @input="(v) => (form.assetsPeriodNumber = v.replace(/[^\d]/g, ''))">
                <template slot="append">/期</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="资产单位" prop="assetsUnit">
              <el-input v-model="form.assetsUnit" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="资产单价" prop="bulMoney">
              <el-input v-model="form.bulMoney" placeholder="请输入"
                @input="(v) => (form.bulMoney = v.replace(/[^\d.]/g, ''))">
                <template slot="append">/元</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="资产图片" prop="assetsImg">
              <image-upload v-model="form.assetsImg" :limit="1" :fileType="['png', 'jpg', 'jpeg']" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog :title="title" :visible.sync="openDetails" width="500px" append-to-body>
      <el-form ref="form1" :model="form1" :rules="rules1" size="mini" label-width="80px">
        <el-form-item label="资产名称" prop="assetsName">
          <el-input v-model="form1.assetsName" placeholder="请输入" disabled="disabled" />
        </el-form-item>
        <el-form-item label="资产型号" prop="assetsModel">
          <el-input v-model="form1.assetsModel" placeholder="请输入" disabled="disabled" />
        </el-form-item>
        <el-form-item label="资产ID" prop="assetsIdCard">
          <el-input v-model="form1.assetsIdCard" placeholder="请输入">
            <template slot="prepend">{{ form1.assetsNo }}-</template>
          </el-input>
        </el-form-item>
        <el-form-item label="购置日期" prop="bulTime">
          <el-date-picker clearable v-model="form1.bulTime" type="date" value-format="yyyy-MM-dd" placeholder="年/月/日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form1.remark" type="textarea" :rows="4" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitDetailsForm">确 定</el-button>
        <el-button @click="cancel1">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog :title="title" :visible.sync="openRecord" width="95%" append-to-body>
      <el-form ref="form2" :model="form2" :rules="rules2" label-width="80px">
        <el-form-item label="资产名称" prop="assetsName">
          <el-input v-model="form1.assetsName" placeholder="请输入" disabled="disabled" />
        </el-form-item>
        <el-form-item label="资产型号" prop="assetsModel">
          <el-input v-model="form1.assetsModel" placeholder="请输入" disabled="disabled" />
        </el-form-item>
        <el-form-item label="资产ID" prop="assetsIdCard">
          <el-input v-model="form1.assetsIdCard" placeholder="请输入">
            <template slot="prepend">{{ form1.assetsNo }}-</template>
          </el-input>
        </el-form-item>
        <el-form-item label="购置日期" prop="bulTime">
          <el-date-picker clearable v-model="form1.bulTime" type="date" value-format="yyyy-MM-dd" placeholder="年/月/日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form1.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitDetailsForm">确 定</el-button>
        <el-button @click="cancel1">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAssets, getAssets, delAssets, addAssets, updateAssets } from "@/api/cld/assets";
import { addDetails } from "@/api/cld/details";

export default {
  name: "Assets",
  dicts: ['cld_assets_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资产信息表格数据
      assetsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        assetsType: null,
        assetsNo: null,
        assetsName: null,
        assetsImg: null,
        assetsModel: null,
        assetsUnit: null,
        assetsBrand: null,
        assetsPeriodNumber: null,
        createId: null,
        createName: null,
        bulMoney: null,
        flag: 0
      },
      // 表单参数
      form: {},
      form1: {},
      // 表单校验
      rules1: {
        bulTime: [
          { required: true, message: "购置日期不能为空", trigger: "blur" }
        ],
        assetsIdCard: [
          { required: true, message: "资产ID不能为空", trigger: "blur" }
        ]
      },
      // 表单校验
      rules: {
        assetsName: [
          { required: true, message: "资产名称不能为空", trigger: "blur" }
        ],
        assetsModel: [
          { required: true, message: "资产型号不能为空", trigger: "blur" }
        ],
        assetsBrand: [
          { required: true, message: "品牌不能为空", trigger: "blur" }
        ],
        assetsType: [
          { required: true, message: "资产类型不能为空", trigger: "blur" }
        ],
        assetsPeriodNumber: [
          { required: true, message: "折旧期数不能为空", trigger: "blur" }
        ],
        assetsUnit: [
          { required: true, message: "资产单位不能为空", trigger: "blur" }
        ],
        assetsImg: [
          { required: true, message: "资产图片不能为空", trigger: "blur" }
        ],
        bulMoney: [
          { required: true, message: "购置单价不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询资产信息列表 */
    getList() {
      this.loading = true;
      listAssets(this.queryParams).then(response => {
        this.assetsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancel1() {
      this.openDetails = false;
      this.reset1();
    },
    // 表单重置
    reset() {
      this.form = {
        assetsId: null,
        assetsType: null,
        assetsNo: null,
        assetsName: null,
        assetsImg: null,
        assetsModel: null,
        assetsUnit: null,
        assetsBrand: null,
        assetsPeriodNumber: null,
        bulMoney: null
      };
      this.resetForm("form");
    },
    // 表单重置
    reset1() {
      this.form1 = {
        assetsId: null,
        assetsNo: null,
        assetsName: null,
        assetsIdCard: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.assetsId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加资产信息";
    },
    /** 新增按钮操作 */
    handleDetailsAdd(row) {
      this.reset1();
      const assetsId = row.assetsId || this.ids
      this.form1.assetsId = assetsId;
      this.form1.assetsName = row.assetsName || '';
      this.form1.assetsNo = row.assetsNo || '';
      this.form1.assetsModel = row.assetsModel || '';
      this.openDetails = true;
      this.title = "新增入库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const assetsId = row.assetsId || this.ids
      getAssets(assetsId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改资产信息";
      });
    },
    /** 资产状况 */
    handleAssetsRecord(row) {
      this.$router.push("/CldAssetsRecord/add-assetsRecord/recordList/" + row.assetsId);
    },
    /** 资产明细 */
    handleAssetsDetails(row) {
      this.$router.push("/CldAssetsDetails/assetsDetails/detailsList/" + row.assetsId);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.assetsId != null) {
            updateAssets(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAssets(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitDetailsForm() {
      this.$refs["form1"].validate(valid => {
        if (valid) {
          addDetails(this.form1).then(response => {
            this.$modal.msgSuccess("入库成功");
            this.openDetails = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const assetsIds = row.assetsId || this.ids;
      this.$modal.confirm('是否确认删除资产信息编号为"' + assetsIds + '"的数据项？').then(function () {
        return delAssets(assetsIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cld/assets/export', {
        ...this.queryParams
      }, `assets_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.el-select {
  width: 100%;
}

.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100%;
}
</style>
